package com.bizdk.generator.controller.gen;

import cn.hutool.core.util.BooleanUtil;
import com.bizdk.generator.common.page.PageResult;
import com.bizdk.generator.common.query.Query;
import com.bizdk.generator.common.utils.R;
import com.bizdk.generator.controller.SimpleController;
import com.bizdk.generator.entity.gen.TypeMapping;
import com.bizdk.generator.service.TypeMappingService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;

/**
 * 字段类型管理
 */
@AllArgsConstructor
@Controller
@RequestMapping("/gen/typemapping")
public class TypeMappingController extends SimpleController<TypeMappingService, TypeMapping> {
    
    @Override
    protected String getPrefix() {
        return "gen/typemapping";
    }
    
    @ResponseBody
    @RequestMapping("/page")
    public R<PageResult<TypeMapping>> page(Query query) {
        PageResult<TypeMapping> page = service.page(query);
        return R.ok(page);
    }
    
    @Override
    public ModelAndView edit(@PathVariable("id") Long id) {
        TypeMapping data = service.getById(id);
        ModelAndView modelAndView = super.edit(id);
        modelAndView.addObject("data", data);
        return modelAndView;
    }
    
    
    @ResponseBody
    @PostMapping("/save")
    public R<String> save(TypeMapping entity) {
        boolean flag = service.save(entity);
        return BooleanUtil.isTrue(flag) ? R.ok() : R.error();
    }
    
    @ResponseBody
    @PutMapping("/update")
    public R<String> update(TypeMapping entity) {
        service.updateById(entity);
        return R.ok();
    }
    
    @ResponseBody
    @PostMapping("/delete")
    public R<String> delete(Long[] ids) {
        service.removeBatchByIds(Arrays.asList(ids));
        return R.ok();
    }
}